<!DOCTYPE html>
<html>
  <head>
    <title>Listing 15.7</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="../scripts/assert.js"></script>
    <link href="../styles/assert.css" rel="stylesheet" type="text/css">
  </head>
  <body>

    <div id="test">                                            <!--#A-->
      <b>Hello</b>, I'm a ninja!
    </div>
    <div id="test2"></div>

    <script>

      (function(){                                                //#B

        var run = 0;

        this.unique = function(array) {                           //#C
          var ret = [];

          run++;                                                  //#D

          for (var i = 0, length = array.length; i < length; i++) {
            var elem = array[i];

            if (elem.uniqueID !== run) {                          //#E
              elem.uniqueID = run;
              ret.push(array[i]);
            }
          }

          return ret;                                             //#F
        };
      })();

      window.onload = function(){                                 //#G
        var divs = unique( document.getElementsByTagName("div") );
        assert( divs.length === 2, "No duplicates removed." );

        var body = unique( [document.body, document.body] );
        assert( body.length === 1, "body duplicate removed." );
      };

    </script>

  </body>
</html>
